package com.koron.order.risk.base.service.impl;


import cn.hutool.core.collection.CollUtil;
import com.koron.order.risk.base.bean.po.LineRiskMeasure;
import com.koron.order.risk.base.mapper.LineRiskMeasureMapper;
import com.koron.order.risk.base.service.LineRiskMeasureService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 风险管控单-管控措施(LineRiskMeasure)表服务实现类
 *
 * @author jzp
 * @since 2022-12-15 16:18:00
 */
@Service
public class LineRiskMeasureServiceImpl implements LineRiskMeasureService {

    /**
     * 通过ID查询单条数据
     *
     */
    @Override
    @TaskAnnotation("getById")
    public LineRiskMeasure getById(SessionFactory factory, String id) {
        LineRiskMeasureMapper mapper = factory.getMapper(LineRiskMeasureMapper.class);
        return mapper.selectById(id);
    }

    @Override
    @TaskAnnotation("getOneByRiskId")
    public LineRiskMeasure getOneByRiskId(SessionFactory factory, String riskId) {
        LineRiskMeasureMapper mapper = factory.getMapper(LineRiskMeasureMapper.class);
        LineRiskMeasure lineRiskMeasure = new LineRiskMeasure();
        lineRiskMeasure.setRiskId(riskId);
        List<LineRiskMeasure> lineRiskMeasures = mapper.selectList(lineRiskMeasure);
        if (lineRiskMeasures != null && lineRiskMeasures.size() > 0) {
            return lineRiskMeasures.get(0);
        }
        return null;
    }

    /**
     * 查询所有数据
     *
     */
    @Override
    @TaskAnnotation("list")
    public List<LineRiskMeasure> list(SessionFactory factory, LineRiskMeasure lineRiskMeasure) {
        LineRiskMeasureMapper mapper = factory.getMapper(LineRiskMeasureMapper.class);
        return mapper.selectList(lineRiskMeasure);
    }

    /**
     * 新增数据
     *
     */
    @Override
    @TaskAnnotation("save")
    public boolean save(SessionFactory factory, LineRiskMeasure lineRiskMeasure) {
        LineRiskMeasureMapper mapper = factory.getMapper(LineRiskMeasureMapper.class);
        if (lineRiskMeasure.getImplemented() == null) {
            lineRiskMeasure.setImplemented(0);
        }
        return mapper.insertOrUpdate(lineRiskMeasure) > 0;
    }

    @Override
    @TaskAnnotation("saveBatch")
    public boolean saveBatch(SessionFactory factory, List<LineRiskMeasure> list) {
        if (CollUtil.isEmpty(list)) {
            return false;
        }
        LineRiskMeasureMapper mapper = factory.getMapper(LineRiskMeasureMapper.class);
        for (LineRiskMeasure lineRiskMeasure : list) {
            if (lineRiskMeasure.getImplemented() == null) {
                lineRiskMeasure.setImplemented(0);
            }
        }
        return mapper.insertOrUpdateBatch(list) > 0;
    }

    /**
     * 修改数据
     *
     */
    @Override
    @TaskAnnotation("update")
    public boolean update(SessionFactory factory, LineRiskMeasure lineRiskMeasure) {
        LineRiskMeasureMapper mapper = factory.getMapper(LineRiskMeasureMapper.class);
        return mapper.update(lineRiskMeasure) > 0;
    }

    /**
     * 通过主键删除数据
     *
     */
    @Override
    @TaskAnnotation("deleteById")
    public boolean deleteById(SessionFactory factory, String id) {
        LineRiskMeasureMapper mapper = factory.getMapper(LineRiskMeasureMapper.class);
        return mapper.hidden(id) > 0;
    }

    @Override
    @TaskAnnotation("removeByRiskId")
    public boolean removeByRiskId(SessionFactory factory, String riskId) {
        LineRiskMeasureMapper mapper = factory.getMapper(LineRiskMeasureMapper.class);
        return mapper.removeByRiskId(riskId) > 0;
    }

}
